import api from '@/api/contact'

const state = {
    rows: [],
    count: 0,
}

const actions = {
    //加载客户信息
    async page({ commit, state }, listQuery = {}) {
        let where = {}
        if (listQuery.customer_id) {
            where.customer_id = listQuery.customer_id
        }
        if (listQuery.name) {
            where.name = {
                $like: `%${listQuery.name}%`
            }
        }
        if (listQuery.tel) {
            where.tel = {
                $like: `%${listQuery.tel}%`
            }
        }

        let response = await api.index({
            page: listQuery.page || 1,
            limit: listQuery.limit || 1000,
            where,
        })
        commit('updateState', response.data)
    },

    //修改客户信息
    async save({ commit, state }, data) {
        let response = await api.save(data)
        return response
    },

    //删除客户信息
    async remove({ commit, state }, id) {
        let response = await api.remove({ id })
        return response
    }
}

const mutations = {
    updateState(state, stateData) {
        Object.assign(state, stateData)
    },
}


export default {
    namespaced: true,
    state,
    actions,
    mutations
}
